package com.park.mer.config;

import cn.hutool.json.JSONUtil;
import com.park.comm.ResultEnum;
import com.park.exception.CommException;
import com.park.json.ResultUtil;
import com.park.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @ClassName ExceptionHandler
 * @Description 统一异常处理
 * @Author zhanghaiwen
 * @Date 2020/1/9 15:11
 * @Version 6.1.1
 **/
@Slf4j
@ControllerAdvice
public class ExceptionHandler {

    /**
     *  拦截Exception类的异常
     * @param e
     * @return
     */
    @org.springframework.web.bind.annotation.ExceptionHandler(Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e){
        //捕获自定义异常
        if(e instanceof CommException){
            CommException commException = (CommException)e;
            log.error("{ErrorMsg}:{}: {ErrorArges}:{}",commException.getErrorMsg(), Arrays.toString(commException.getArgs()));
            return ResultUtil.SUCCESS(commException);
        }
        //抛出系统异常
        log.info("------------------异常处理--------------------");
        log.error("{系统异常}:{}:{}",e.getMessage(),e);
        log.info("------------------异常处理--------------------");
        return ResultUtil.SUCCESS(ResultEnum.SYS_ERROR);
    }

    /* *
     * @Author zhanghaiwen
     * @Description MethodArgumentTypeMismatchException 异常捕获
     * @Date 11:09 2019/10/24
     * @Param [e]
     * @return com.biyouche.comm.base.ResultResponse
     */
    @org.springframework.web.bind.annotation.ExceptionHandler(MethodArgumentTypeMismatchException.class)
    @ResponseBody
    public Result methodArgumentTypeMismatchExceptionHandler(Exception e){
        //捕获自定义异常
        if(e instanceof MethodArgumentTypeMismatchException){
            MethodArgumentTypeMismatchException commException = (MethodArgumentTypeMismatchException)e;
            String param = commException.getName();
            MethodParameter parameter = commException.getParameter();
            String methodName = parameter.getMethod().getName();
            String className = parameter.getDeclaringClass().getName();
            HashMap<String,String> parMap = new HashMap<String,String>();
            parMap.put("className",className);
            parMap.put("methodName",methodName);
            parMap.put("param",param);
            log.info("------------------请求参数异常处理--------------------");
            log.error("请求参数异常="+ JSONUtil.toJsonStr(param));
            log.info("------------------请求参数异常处理--------------------");
            return ResultUtil.SUCCESS(ResultEnum.SYS_ERROR);
        }
        //抛出系统异常
        log.info("------------------请求参数异常处理--------------------");
        log.error("{系统异常}:" , e.getMessage());
        log.info("------------------请求参数异常处理--------------------");
        return ResultUtil.SUCCESS(ResultEnum.SYS_ERROR);
    }

}
